/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mocks;



/**
 *
 * @author User
 */
import appClient.ConnectClient;
import appClient.VisualClien;
import clientListener.AppClientListener;
import clientListener.ClientListener;
import com.google.gson.Gson;
import engine.ClientFactory;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.parser.ParseException;
import protocol.ClientProtocol;
import protocol.Command;
import webClient.HttpClient.SocketProcessor;

public class MockClient extends Thread {
  private int port;
  private String login; 
  private String pass; 
  private String addr;
  private Socket socket;
  private BufferedReader in;
  private PrintWriter out;
  public javax.swing.JPanel FieldPannel;
  public VisualClien vis;
  public ClientProtocol clientProtocol;
  public String role;
  public String Uid;
  public ClientListener clilist;
  public SocketProcessor socProc;
  public String str = null;

 
  
  public MockClient(String login, String pass, String typeClient) throws IOException{
    this.port = port;
    this.login = login;
    this.pass = pass;
    this.addr = addr;
    addr = "localhost";
    port = 8888;
    
 
    
    System.out.println("addr = " + addr);
    socket = new Socket(addr, port);
    System.out.println("socket = " + socket);
    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
    clilist = ClientFactory.getClient(typeClient);
    
    start();
    
  }
  
  
  public void refuse(String send){
     out.println(send);
  }
  
  public String getLine(String str){
     return str;
  }
  
  public void run()
  {
    try {
          Command obj = new Command();
          obj.Type = "Connect";
          obj.Data = login;
          obj.Add = pass;
          Gson gson = new Gson();
          String send = gson.toJson(obj);
      out.println(send);
      
      while (true)
            {
                
        try {
          str = in.readLine();
         //  System.out.println("Mock Client comming string: " + str);
        } catch (IOException ex) {
          Logger.getLogger(ConnectClient.class.getName()).log(Level.SEVERE, null, ex);
        }
                if (str.equals("{\"Type\": \"End\"}"))
                {
                  out.println("{\"Type\": \"End\"}");
                  break;
                }
            }
      
    } finally {
      System.out.println("closing ConnectClient ...");
      try {
        socket.close();
      } catch (IOException ex) {
        Logger.getLogger(ConnectClient.class.getName()).log(Level.SEVERE, null, ex);
      }
    }

  }

  
}

